package com.lzj.jiangxindaojia.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.lzj.jiangxindaojia.MainActivity;
import com.lzj.jiangxindaojia.R;
import com.lzj.jiangxindaojia.utils.LogUtils;
import com.lzj.jiangxindaojia.utils.Utils;
import com.lzj.jiangxindaojia.wxutils.Constants;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		LogUtils.log("onPayFinish, errCode = " + resp.errCode);

//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
//		}

		int code = resp.errCode;

		switch (code){
			case 0://支付成功后的界面
                LogUtils.log("resp.errCode "+resp.errCode);
				MainActivity.webView.loadUrl("javascript: appSuccessGo()");
				break;
			case -1:
                LogUtils.log("resp.errCode "+resp.errCode);
				Utils.showToast(getApplicationContext(), "签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等。");
				break;
			case -2://用户取消支付后的界面
                LogUtils.log("resp.errCode "+resp.errCode);
				break;
		}
		finish();
		//微信支付后续操作，失败，成功，取消
	}
}